from db_init import db
from datetime import datetime
from flask_login import UserMixin

class User(db.Model,UserMixin):
    id = db.Column(db.Integer,primary_key=True)
    login_time = db.Column(db.DateTime,default=datetime.now)
    img = db.Column(db.String(2048))
    password = db.Column(db.String(1024))
    make_password = db.Column(db.String(16))
    mobile = db.Column(db.String(11))
    address = db.Column(db.String(42))
    real_name = db.Column(db.String(42))
    id_card = db.Column(db.Integer)
    landlord = db.Column(db.Boolean,server_default='0')

class House(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(16))
    info = db.Column(db.String(1024))
    # 是否整套出租
    zheng = db.Column(db.Boolean)
    # 人数
    p_num = db.Column(db.Integer)
    # 家具数量
    c_num = db.Column(db.Integer)
    #和合同形成关系
    contract_id = db.Column(db.ForeignKey('contract.id'))
    # 房间的主人
    master_id = db.Column(db.ForeignKey('user.id'))
    # 租客
    tenantid = db.Column(db.ForeignKey('user.id'))
    #有没有人
    live = db.Column(db.Boolean)
    city = db.Column(db.String(16))
    price = db.Column(db.Integer)
    order_id = db.relationship('Order',backref='house')

# 合同表
class Contract(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    house_id = db.relationship('House',backref='contract')
    price = db.Column(db.Integer)

# 多对多关系表
house_facility = db.Table(
    'house_facility',
    db.Column('house_id',db.ForeignKey('house.id'), primary_key=True),
    db.Column('facility_id',db.ForeignKey('facility.id'), primary_key=True)
)

class Facility(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(16))
    icon = db.Column(db.String(1024))
    house_id = db.relationship('House',secondary=house_facility,backref='facility',)

# 订单
class Order(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    num = db.Column(db.Integer,unique=True)
    name = db.Column(db.String(16))
    # 房屋id
    house_id = db.Column(db.ForeignKey('house.id'))
    # 租客的人
    tenant_id = db.Column(db.ForeignKey('user.id'))
    start_time = db.Column(db.Integer)
    end_time = db.Column(db.Integer)
    type = db.Column(db.Enum(
        '待接单',
        '待支付',
        '完成',
        '已拒绝',
    ),default='待接单')


class Advertise(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(16))
    img = db.Column(db.String(2048))
    url = db.Column(db.String(1024))
    house_id = db.Column(db.ForeignKey('house.id'))

class Img(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(16))
    url = db.Column(db.String(2048))
    house_id = db.Column(db.ForeignKey('house.id'))

# 突然要加要求
class Comment(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.String(1024))
    # 房屋id
    house_id = db.Column(db.ForeignKey('house.id'))
    # 接单人id
    root_id = db.Column(db.Integer)
    beau = db.Column(db.ForeignKey('comment.id'))
    reply = db.relationship('Comment',remote_side=[id],backref='beau_id')
    author_id = db.Column(db.ForeignKey('user.id'))
    num = db.Column(db.Integer,default=0)



